import scrapy
from scrapy import Request
from ..items import QidianHotItem
from scrapy.loader import ItemLoader

class QidianSpider(scrapy.Spider):
    name = 'hot'
    allowed_domains = ['qidian.com']
    # start_urls = ['https://www.qidian.com/rank/hotsales?style=1']
    current_page = 1

    def start_requests(self):
        url = "https://www.qidian.com/rank/hotsales?style=1&page=1"
        yield Request(url, callback=self.qidian_parse)

    def qidian_parse(self, response):
        list_selector = response.xpath("//div[@class='book-mid-info']")
        for one_selector in list_selector:
            novel = ItemLoader(item=QidianHotItem(), selector=one_selector)
            novel.add_xpath("name", "h4/a/text()")
            novel.add_xpath("author", "p[1]/a[1]/text()")
            novel.add_xpath("type", "p[1]/a[2]/text()")
            novel.add_xpath("form", "p[1]/span/text()")
            yield novel.load_item()
        self.current_page += 1
        if self.current_page <= 5:
            next_url = 'https://www.qidian.com/rank/hotsales?style=1&page=%d' % self.current_page
            yield Request(next_url, callback=self.qidian_parse)
